package com.summer.rx;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class ThreadControl {

	//这个东西，一定要记住，我先简单给你领个入门关于JAVA并发编程的东西，你看着以后受益很多
	private final BlockingQueue<Runnable> workerQueue = new ArrayBlockingQueue<Runnable>(10);

	public ExecutorService executorService = Executors.newFixedThreadPool(1);
	
	public void registered(Runnable runnable){
		workerQueue.add(runnable);
		//链式编程这里应该返回原对象
	}
	
	public void doAction() throws InterruptedException{
		Runnable runnable = workerQueue.take();
		executorService.execute(runnable);
	}
	
	public void switchThread(){
		executorService = Executors.newFixedThreadPool(1, new ThreadFactory(){
			@Override
			public Thread newThread(Runnable r) {
				return new Thread(r);
			}
		});
	}

}
